package com.viewpagertext.DBHelper;

import androidx.fragment.app.FragmentActivity;
import androidx.room.Database;
import androidx.room.Room;
import androidx.room.RoomDatabase;

import com.viewpagertext.DBHelper.Dao.DownloadMusicDao;
import com.viewpagertext.DBHelper.Dao.PlaybackModeDao;
import com.viewpagertext.DBHelper.Dao.RecentPlayDao;
import com.viewpagertext.DBHelper.Dao.SearchHistoryDao;
import com.viewpagertext.DBHelper.Dao.StarredSongDao;
import com.viewpagertext.DBHelper.Dao.UserDao;
import com.viewpagertext.Model.DownloadMusic;
import com.viewpagertext.Model.User;
import com.viewpagertext.Model.RecentPlay;
import com.viewpagertext.app.ScrollShapeUIApplication;
import com.viewpagertext.json.PlaybackMode;
import com.viewpagertext.json.StarredSong;  // 确保引入正确的 StarredSong 类
import com.viewpagertext.Model.SearchHistory;

/**
 * 用于提供应用中所有数据库操作的核心类。
 */
@Database(entities = {SearchHistory.class, StarredSong.class, PlaybackMode.class, DownloadMusic.class, RecentPlay.class, User.class}, version = 9, exportSchema = false)
public abstract class AppDatabase extends RoomDatabase {

    private static volatile AppDatabase INSTANCE;

    /**
     * 使用单例模式获取 AppDatabase 实例。
     *
     * @return 返回 AppDatabase 的单例实例。
     */
    public static AppDatabase getInstance() {
        if (INSTANCE == null) {
            synchronized (AppDatabase.class) {
                if (INSTANCE == null) {
                    INSTANCE = Room.databaseBuilder(
                                    ScrollShapeUIApplication.context,
                                    AppDatabase.class,
                                    "music.db"
                            )
                            .allowMainThreadQueries()  // 注意：允许在主线程查询数据库通常不推荐，应考虑使用异步处理
                            .fallbackToDestructiveMigration()  // 添加此行以处理数据库版本变化时的数据迁移
                            .build();
                }
            }
        }
        return INSTANCE;
    }

    // DAOs
    public abstract SearchHistoryDao searchHistoryDao();

    public abstract StarredSongDao starredSongDao();

    public abstract PlaybackModeDao playbackModeDao();

    public abstract DownloadMusicDao downloadMusicDao();

    public abstract UserDao UserDao();

    public abstract RecentPlayDao recentPlayDao();
}